package com.mak001.lite.multiSkill.GUI.Tabs;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.Tile;

public class MinerTab extends JPanel {
	private static final long serialVersionUID = 1L;

	private JTextField textField15;
	private JComboBox minePlaceDrop;
	private JTextField textField14;
	private JComboBox oreToMine;
	private JCheckBox checkBox1;

	String[] Places = { "draynor", "varrock" };// to match rune ess

	String[] ores = { "rune essence", "clay", "copper", "tin", "iron",
			"silver", "coal", "gold", "mithril", "adamant", "runite" };
	int[] copper = { 3027, 3229, 5779, 5780, 5781, 9708, 9709, 9710,
			11936, 11937, 11938, 11960, 11961, 11962 };
	int[] clay = { 9711, 9712, 9713, 15503, 15504, 15505, 10577, 10578, 10579 };
	int[] tin = {  3038, 3245, 5776, 5777, 5778, 9714, 9715, 9716, 11933, 11934, 11935,
			11957, 11958, 11959 };
	int[] iron = { 5773, 5774, 5775, 9717, 9718, 9719, 37307, 37308, 37309,
			11954, 11956, 11956 };
	int[] silver = { 37304, 37305, 37306, 11948 };
	int[] gold = { 5768, 5769, 9720, 9721, 9722, 37310, 37311, 37312 };
	int[] coal = { 3032, 3233, 3281, 5770, 5771, 5772, 11930, 11931, 11932 };
	int[] mith = { 3041, 3280, 5784, 5785, 5786, 11942, 11943, 11944 };
	int[] addy = { 3040, 3273, 5728 };

	private String[] getMinePlaces(String s) {
		if (s.contains("rune")) {
			return new String[] { "draynor", "varrock" };
		} else if (s.contains("clay")) {
			return new String[] { "sw varrock", "dwarven mine", "rimmington",
					"barbarian village" };
		} else if (s.contains("tin")) {
			return new String[] { "barbarian village", "al-kharid mine",
					"edgeville", "se lumbridge", "se varrock", "sw varrock",
					"dwarven mine", "rimmington" };
		} else if (s.contains("copper")) {
			return new String[] { "al-kharid mine", "edgeville",
					"se lumbridge", "se varrock", "dwarven mine", "rimmington" };
		} else if (s.contains("iron")) {
			return new String[] { "al-kharid mine", "edgeville", "se varrock",
					"sw varrock", "dwarven mine", "rimmington" };
		} else if (s.contains("silver")) {
			return new String[] { "al-kharid mine", "edgeville", "sw varrock",
					"rimmington" };
		} else if (s.contains("coal")) {
			return new String[] { "barbarian village", "al-kharid mine",
					"edgeville", "sw lumbridge", "se varrock", "sw varrock",
					"dwarven mine", "mining guild" };
		} else if (s.contains("gold")) {
			return new String[] { "al-kharid mine", "dwarven mine",
					"rimmington" };
		} else if (s.contains("mithril")) {
			return new String[] { "al-kharid mine", "edgeville",
					"sw lumbridge", "dwarven mine", "mining guild" };
		} else if (s.contains("adamant")) {
			return new String[] { "al-kharid mine", "edgeville",
					"sw lumbridge", "dwarven mine" };
		} else if (s.contains("runite")) {
			return new String[] { "N/A" };
		} else {
			return new String[] { "MAK" };
		}
		// TODO
	}

	public MinerTab() {
		checkBox1 = new JCheckBox();
		textField15 = new JTextField();
		minePlaceDrop = new JComboBox();
		textField14 = new JTextField();
		oreToMine = new JComboBox();
		this.setLayout(null);

		// ---- textField14 ----
		textField14.setText("Ores to Mine");
		textField14.setEditable(false);
		textField14.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(textField14);
		textField14.setBounds(35, 30, 115,
				textField14.getPreferredSize().height);

		// ---- oreToMine ----
		oreToMine.setModel(new DefaultComboBoxModel(ores));
		oreToMine.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				placeChange(e);
			}
		});
		this.add(oreToMine);
		oreToMine.setBounds(35, 55, 115, oreToMine.getPreferredSize().height);

		// ---- textField15 ----
		textField15.setText("Place to Mine");
		textField15.setEditable(false);
		textField15.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(textField15);
		textField15.setBounds(200, 30, 145,
				textField15.getPreferredSize().height);

		// ---- minePlaceDrop ----
		minePlaceDrop.setModel(new DefaultComboBoxModel(Places));
		this.add(minePlaceDrop);
		minePlaceDrop.setBounds(200, 55, 145,
				minePlaceDrop.getPreferredSize().height);

		// ---- checkBox1 ----
		checkBox1.setText("Power Mine");
		this.add(checkBox1);
		checkBox1.setBounds(135, 5, 105, checkBox1.getPreferredSize().height);

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for (int i = 0; i < this.getComponentCount(); i++) {
				Rectangle bounds = this.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width,
						preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height,
						preferredSize.height);
			}
			Insets insets = this.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			this.setMinimumSize(preferredSize);
			this.setPreferredSize(preferredSize);
		}
	}

	private void placeChange(ItemEvent e) {
		String s = ((JComboBox) e.getSource()).getSelectedItem().toString();
		minePlaceDrop.setModel(new DefaultComboBoxModel(getMinePlaces(s)));
	}

	public int getMiner() {
		String s = minePlaceDrop.getSelectedItem().toString();
		String s0  =oreToMine.getSelectedItem().toString();
		String[] s1 = { "edgeville", "dwarven mine", "mining guild" };
		String[] runeEss = {"rune"};
		if (checkBox1.isSelected()) { // power mine
			return 2;
		} else {
			if (contains(s0, runeEss)) {// rune ess
				return 0;
			} else if (contains(s, s1)) {// stair miner
				return 1;
			} else { // normal miner
				return 3;
			}
		}
	}

	private boolean contains(String s, String[] s1) {
		for (String d : s1) {
			if (s.contains(d)) {
				return true;
			}
		}
		return false;
	}

	public Area getPlace() {
		String s = minePlaceDrop.getSelectedItem().toString();
		if (s.contains("varrock")) {//draynor", "
			return new Area(new Tile(3249, 3398), new Tile(3256, 3405));
		} else {
			return new Area(new Tile(3096, 9566), new Tile(3107, 9574));
		}
	}

	public int[] getRocks() {
		String s = oreToMine.getSelectedItem().toString();
		if (s.contains("clay")) {
			return clay;
		} else if (s.contains("copper")) {
			return copper;
		} else if (s.contains("tin")) {
			return tin;
		} else if (s.contains("iron")) {
			return iron;
		} else if (s.contains("silver")) {
			return silver;
		} else if (s.contains("coal")) {
			return coal;
		} else if (s.contains("gold")) {
			return gold;
		} else if (s.contains("mith")) {
			return mith;
		} else if (s.contains("adam")) {
			return addy;
		} else {
			return null;
		}
	}

	public Area getMiningArea() {
		String s = minePlaceDrop.getSelectedItem().toString();
		String ore = oreToMine.getSelectedItem().toString();
		if (s.contains("al-kharid mine")) {
			return new Area(new Tile(3288, 3286), new Tile(3308, 3320));
		} else if (s.contains("se varrock")) {
			return new Area(new Tile(3277, 3359), new Tile(3292, 3372));
		} else if (s.contains("sw varrock")) {
			return new Area(new Tile(3171, 3363), new Tile(3185, 3380));
		} else if (s.contains("barbarian village")) {
			if (ore.contains("clay")) {
				return new Area(new Tile(3079, 3397), new Tile(3085, 3401));
			} else {
				return new Area(new Tile(3077, 3416), new Tile(3085, 3424));
			}
		} else if (s.contains("rimmington")) {
			return new Area(new Tile(2965, 3227), new Tile(2991, 3253));
		} else if (s.contains("sw lumbridge")) {
			return new Area(new Tile(3140, 3140), new Tile(3153, 3253));
		} else if (s.contains("se lumbridge")) {
			return new Area(new Tile(3221, 3143), new Tile(3236, 3153));
		}
		// TODO
		return null;
	}

	public String getSPlace() {
		return minePlaceDrop.getSelectedItem().toString();
	}

}
